package com.pine.dcts.handler;

import com.alibaba.fastjson.JSON;
import com.pine.dcts.common.BaseHandler;
import com.pine.dcts.common.Result;
import com.pine.dcts.entity.JobInfo;
import com.pine.dcts.model.JobVO;
import com.pine.dcts.query.JobQuery;
import com.pine.dcts.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RequestMapping("/job")
@RestController
public class JobHandler extends BaseHandler {
    @Autowired
    private JobService jobService;

    @PostMapping("/add")
    public Result add(@RequestBody  String job){
     JobVO jobVO =    JSON.parseObject(job, JobVO.class);
     jobService.addJob(jobVO);
     return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody  String job){
        JobVO jobVO =    JSON.parseObject(job, JobVO.class);
        jobService.updateJob(jobVO);
        return Result.success();
    }

    @PostMapping("/delete")
    public Result delete(@NotNull(message = "id不能为空") Integer id){
        jobService.deleteJob(id);
        return Result.success();
    }
    @GetMapping("/get")
    public Result<JobVO> get(@NotNull(message = "id不能为空") Integer id){
        return Result.success(jobService.getJobVOById(id));
    }

    @GetMapping("/page")
    public Result<JobInfo> page(@Validated JobQuery query){
        return Result.success(jobService.page(query));
    }
}
